﻿using Elasticsearch.Net;
using System;

namespace IceDog.ElasticSearchClient.MSTest.ESNet
{
    public static class ESNetClientProvider
    {
        private static ElasticLowLevelClient _elasticLowLevelClient;
        private static ElasticLowLevelClient _elasticLowLevelClientPool;
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static ElasticLowLevelClient GetElasticLowLevelClient()
        {
            if (_elasticLowLevelClient != null)
            {
                return _elasticLowLevelClient;
            }

            var node = new Uri("http://localhost:9201");
            var config = new ConnectionConfiguration(node);
            config.RequestTimeout(TimeSpan.FromMinutes(2));
            var client = new ElasticLowLevelClient(config);
            _elasticLowLevelClient = client;
            return _elasticLowLevelClient;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static ElasticLowLevelClient GetElasticLowLevelClientWithPool()
        {
            if (_elasticLowLevelClientPool != null)
            {
                return _elasticLowLevelClientPool;
            }

            var uris = new[]
            {
                new Uri("http://localhost:9200"),
                new Uri("http://localhost:9201"),
                new Uri("http://localhost:9202")
            };
            var connectionPool = new SniffingConnectionPool(uris);
            var config = new ConnectionConfiguration(connectionPool);
            config.RequestTimeout(TimeSpan.FromMinutes(2));
            var client = new ElasticLowLevelClient(config);
            _elasticLowLevelClientPool = client;
            return _elasticLowLevelClientPool;
        }
    }
}
